import { cartMutations, productMutations } from './mutations'

import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import { productGetters } from './getters'

Vue.use(Vuex)

const API_BASE = 'http://localhost:3000/api/v1'

export default new Vuex.Store({
    strict: true,
    state: {
        // 全局购物车
        cart: [],
        showLoader: false,
        product: {},
        products: [],
        manufacturers: []
    },
    mutations: {
        ...productMutations,
        ...cartMutations
    },
    getters: {
        ...productGetters
    },
    actions: {
        allProducts({ commit }) {
            commit('ALL_PRODUCTS')

            axios.get(`${API_BASE}/products`).then(response => {
                console.log('response', response)
                commit('ALL_PRODUCTS_SUCCESS', {
                    products: response.data
                })
            })
        },
        productById({ commit }, payload) {
            commit('PRODUCT_BY_ID')

            const { productId } = payload
            axios.get(`${API_BASE}/products/${productId}`).then(response => {
                commit('PRODUCT_BY_ID_SUCCESS', {
                    product: response.data
                })
            })
        }
    }
})
